package com.wdm.sms.controller.user;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wdm.sms.commonutil.enums.ResponseEnum;
import com.wdm.sms.commonutil.utils.jwt.PassToken;
import com.wdm.sms.commonutil.config.exception.MyException;
import com.wdm.sms.commonutil.utils.r.R;
import com.wdm.sms.commonutil.utils.result.Result;
import com.wdm.sms.entity.common.dto.user.UserDTO;
import com.wdm.sms.entity.common.dto.page.PageDTO;
import com.wdm.sms.entity.User;
import com.wdm.sms.entity.common.vo.user.LoginVo;
import com.wdm.sms.entity.common.vo.user.UpdatePasswordUserVO;
import com.wdm.sms.service.redis.RedisService;
import com.wdm.sms.service.user.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wudaiming
 * @since 2023-03-03
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/user")
public class UserController {
    private final UserService userService;
    private final RedisService redisService;
    @PostMapping("/login")
    //@CostTime
    @PassToken
    public Result login(@RequestBody LoginVo loginVo) {
        //JSONObject object = new JSONObject();
        return userService.login(loginVo);
    }

    @GetMapping("/loginOut")
    @PassToken
    public boolean loginOut(@RequestParam String id) {
        boolean result = redisService.delete(id);
        if (!redisService.delete(id)) {
            throw new MyException(ResponseEnum.USER_EX);
        }

        return result;
    }

    @PostMapping("/updatePassword")
    public R updatePassword(@RequestBody UpdatePasswordUserVO updatePasswordUserVO) {
        return userService.updatePassword(updatePasswordUserVO);
    }

    @PostMapping("/getAllUser")
    public R<Page<User>> getAllUsers(@RequestBody PageDTO pageDTO){
        return userService.getAllUsers(pageDTO);
    }
    @PostMapping("/addUser")
    public Result addUser(@RequestBody UserDTO userDTO){
        return userService.addUser(userDTO);
    }
    @PostMapping("/updateUser")
    public Result updateUser(@RequestBody UserDTO userDTO){
        return userService.updateUser(userDTO);
    }
    @GetMapping("/deleteUser")
    public Result deleteUser(@RequestParam String id){
        return userService.deleteUser(id);
    }
}
